package com.example.service;



import com.example.bean.SysAllDict;
import com.example.bean.SysDict;
import com.example.bean.SysProduct;
import com.example.bean.vo.ProductTypeVO;
import com.example.bean.vo.ProvinceCityAreaVO;
import com.example.dao.SysAllDictRepository;
import com.example.dao.SysDictRepository;
import com.example.dao.SysProductRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;


@Service
public class SysDictService {

    @Autowired
    private SysDictRepository sysDictRepository;
    @Autowired
    private SysAllDictRepository sysAllDictRepository;
    @Autowired
    private SysProductRepository sysProductRepository;


    public List<SysDict> getProvinceAndCity(){
        List<String> keys = new ArrayList<>();
        keys.add("PROVINCE");
        keys.add("CITY");
        return sysDictRepository.findByDictKeyIn(keys);
    }

    public List<ProvinceCityAreaVO> getProvinceCityAreaVO(List<SysDict> sysDicts, Long parentId) {
        List<ProvinceCityAreaVO> children = new ArrayList<>();
        for (SysDict sysDict : sysDicts) {
            Long id = sysDict.getId();
            Long pid = sysDict.getParentId();
            if (parentId.equals(pid)) {
                List<ProvinceCityAreaVO> childNode = getProvinceCityAreaVO(sysDicts, id);
                ProvinceCityAreaVO provinceCityAreaVO = new ProvinceCityAreaVO();
                provinceCityAreaVO.setValue(sysDict.getDictValue());
                provinceCityAreaVO.setLabel(sysDict.getDictValue());
                if(childNode != null && childNode.size() > 0){
                    provinceCityAreaVO.setChildren(childNode);
                }
                children.add(provinceCityAreaVO);
            }
        }
        return children;
    }
    public List<ProductTypeVO> getProductVO(List<SysProduct> sysDicts, Long parentId) {
        List<ProductTypeVO> children = new ArrayList<>();
        for (SysProduct sysDict : sysDicts) {
            Long id = sysDict.getId();
            Long pid = sysDict.getParentId();
            if (parentId.equals(pid)) {
                List<ProductTypeVO> childNode = getProductVO(sysDicts, id);
                ProductTypeVO provinceCityAreaVO = new ProductTypeVO();
                provinceCityAreaVO.setValue(sysDict.getDictValue());
                provinceCityAreaVO.setLabel(sysDict.getId().toString());
                if(childNode != null && childNode.size() > 0){
                    provinceCityAreaVO.setChildren(childNode);
                }
                children.add(provinceCityAreaVO);
            }
        }
        return children;
    }
    public List<SysProduct> getProduct(){
        List<String> keys = new ArrayList<>();
        keys.add("firsttype");
        keys.add("secondtype");
        return sysProductRepository.findByDictKeyIn(keys);
    }

	public Map<String,List<String>> getAllDict() {
		List<SysAllDict> dtos=sysAllDictRepository.findAll();
		Map<String,List<String>> results=new HashMap<String,List<String>>();
		
		for(SysAllDict dict:dtos) {
			if(results.containsKey(dict.getCode())) {
				List<String> re=results.get(dict.getCode());
				re.add(dict.getDictKey());
				results.put(dict.getCode(), re);
			}
			else {
				List<String> re=new ArrayList<String>();
				re.add(dict.getDictKey());
				results.put(dict.getCode(), re);
			}
		}
		return results;
	}
}
